import store from './index'

export default {
    state: {
        airCityPlayer: null,
        aircityApi: null,
        HostConfig: {
            instanceId: '',
            MatchServer: '192.168.5.3:82',
            Token: '',
            Path: 'http://192.168.5.3:84/images/acp.png'
        }
    },
    mutations: {
        setaircityApi(state, aircityApi) {
            state.aircityApi = aircityApi
        },
        setairCityPlayer(state, airCityPlayer) {
            state.airCityPlayer = airCityPlayer
        },
        initInterface(withPlayer, withApi) {
            //AirCityPlayer
            if (withPlayer) {
                let options = {
                    domId: 'player',
                    token: HostConfig.Token,
                    showMarker: true,
                    showStartupInfo: true,
                }
                this.aircityPlayer = new AirCityPlayer(HostConfig.instanceId || HostConfig.AirCityPlayer, options)
                this.$store.commit('setairCityPlayer', this.aircityPlayer)
                // let p_bitrate = getQueryVariable('bitrate') //2021.04.16 Add 从参数中解析码率
                // p_bitrate && aircityPlayer.setBitrate(p_bitrate)
            }

            //AirCityAPI
            if (withApi) {
                this.aircityApi = new AirCityAPI(HostConfig.instanceId || HostConfig.AirCityAPI, {
                    onReady: () => {
                        //此时可以调用接口了
                        __g.camera.get(r => {
                            this.log(`Camera: ${r.x}, ${r.y}, ${r.z}, ${r.pitch}, ${r.yaw}`)
                        })
                    },
                    onApiVersion: (vNo, vType) => {
                        var spanVer = document.getElementById('spanVer')
                        if (spanVer) {
                            if (__g.misc.isApiVersionMatched()) {
                                spanVer.innerHTML = '<font color=green>' + __g.misc.apiVersion + '</font>'
                            } else {
                                spanVer.innerHTML = 's:<font color=red>' + __g.misc.apiVersionServer + '</font>-c:' + __g.misc.apiVersion
                                logWithColor('red', '<b>JS SDK版本和云渲染服务器版本不一致，可能造成接口调用错误，请确认!</b>')
                                log(spanVer.innerHTML)
                                log('')
                            }
                        }
                    },
                    onEvent: e => {
                        this.event(e)
                    },
                    onLog: this.log(),
                    useColorLog: true, //仅用于SDK测试页面，二次开发请设置为false
                })
                this.$store.commit('setaircityApi', this.aircityApi)
            }
        },
        log(e) {
            if (e) console.log('可以调用api了', e)
        },
        getMatchServerConfig(host, fn, callbackIndex) {
            if ('WebSocket' in window) {
                var url = `ws://${host}`
                let __fn = fn

                var ws = new WebSocket(url)
                ws.onopen = function () {
                    this.send(
                        JSON.stringify({
                            command: 0,
                            callbackIndex: callbackIndex,
                        })
                    )
                }
                ws.onmessage = function (event) {
                    var o = JSON.parse(event.data)
                    __fn(o)
                }
                ws.onclose = function () { }
                ws.onerror = function (event) { }
            } else {
                this.log('Not Support WebSocket!')
            }
        },
        init(withPlayer, withInterface) {
            //页面地址加参数： http://192.168.1.222/int.html?ms
            let _this = this
            this.getMatchServerConfig(HostConfig.MatchServer, function (o) {
                if (o.result == 0) {
                    HostConfig.instanceId = o.instanceId
                    _this.initInterface(withPlayer, withInterface)
                } else {
                    _this.initInterface(withPlayer, withInterface)
                }
            })
        },
    },
}